package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CommonFormListDTO;
import com.xbongbong.paas.pojo.dto.CommonFormSaveDTO;
import com.xbongbong.paas.pojo.vo.CommonFormListVO;
import com.xbongbong.paas.pojo.vo.CommonFormSaveVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.CommonFormService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/8/29 20:02
 * @desrcption: 常用表单对象
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/common")
public class MobileCommonFormController {


    @Resource
    private CommonFormService commonFormService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 搜索常用表单
     * @param commonFormListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/form/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String commonFormList(@RequestBody @Valid CommonFormListDTO commonFormListDTO, BindingResult br) {
        XbbResponse<CommonFormListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommonFormListVO commonFormListVO = commonFormService.commonFormList(commonFormListDTO);
                response = new XbbResponse<>(commonFormListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response);
    }

    /**
     * 搜索快捷新建表单
     * @param commonFormListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/quickNew/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String quickNewList(@RequestBody @Valid CommonFormListDTO commonFormListDTO, BindingResult br) {
        XbbResponse<CommonFormListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommonFormListVO commonFormListVO = commonFormService.quickNewList(commonFormListDTO);
                response = new XbbResponse<>(commonFormListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response);
    }

    /**
     * 设置常用表单
     * @param commonFormSaveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/form/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String commonFormSave(@RequestBody @Valid CommonFormSaveDTO commonFormSaveDTO, BindingResult br) {
        XbbResponse<CommonFormSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(commonFormSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(commonFormSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                CommonFormSaveVO commonFormSaveVO = commonFormService.save(commonFormSaveDTO);
                response = new XbbResponse<>(commonFormSaveVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response);
    }
}
